package sg.edu.nus.iss.vmcs.machinery;

/*
 * Copyright 2011 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

import java.awt.*;
import java.util.Observable;
import java.util.Observer;

import sg.edu.nus.iss.vmcs.store.*;
import sg.edu.nus.iss.vmcs.util.*;

/**
 * This class instantiates the boundary object that displays the contents of store.
 * These are Drinks Store and Cash Store and the store viewer allows them to be changed.
 * This class inherits from Java AWT Panel class.
 * @version 1.0  07/12/2011
 * @author Team 7S - SE19
 */

public class StoreViewer extends Panel implements Observer {

	private LabelledDisplay viewItems[];
	private StoreController storeCtrl;
	
	private int type;
/**
 * Title for Cash Store
 */	
	public static final String CASH_VIEW_TITLE = "Quantity of Coins Available";
/**
 * Title for Drinks Store
 */
	public static final String DRINK_VIEW_TITLE = "Quantity of Drinks Available";

/**
 * This constructor will create a new instance of the store viewer
 * @param ti
 * @param sctrl
 */	
	public StoreViewer(int ti, StoreController sctrl) {
		
		storeCtrl = sctrl;
		type = ti;
		
		String title = null;
		switch (type) {
		case Store.CASH:
			title = CASH_VIEW_TITLE;
			break;
		case Store.DRINK:
			title = DRINK_VIEW_TITLE;
			break;
		}

		Panel pl = new Panel(new FlowLayout(FlowLayout.LEFT));
		pl.add(new Label(title));

		int sSize = storeCtrl.getStoreSize(type);
		viewItems = new LabelledDisplay[sSize];

		StoreItem[] storeItem = storeCtrl.getStoreItems(type);
		this.setLayout(new GridLayout(0, 1));
		this.add(pl);

		for (int i = 0; i < sSize; i++) {
			String name = storeItem[i].getContent().getName();
			viewItems[i] = new LabelledDisplay(name,
						LabelledDisplay.DEFAULT,
						LabelledDisplay.GRID);
			viewItems[i].addListener(
                        new StoreViewerListener(type, i, storeCtrl));
			this.add((Panel)viewItems[i].getLabelledDisplay());
		}
		
		update();
	}

/**
 * This operation updates view items for Drinks Store and Cash Store
 * @see StoreItem
 * @see Store
 * @see StoreObject 
 */
	
	public void update () {
		System.out.println("StoreViewer Update (1-case,2-Drink): " + type);
		StoreItem[] storeItem = storeCtrl.getStoreItems(type);
		for (int i = 0; i < storeItem.length; i++) {
			int val = storeItem[i].getQuantity();
			String sval = String.valueOf(val);
			System.out.println("sval["+i+"] :" + sval);
			viewItems[i].setValue(sval);
		}
	}

/**
 * This operation updates view items for drinks store and cash store for
 * a particular store object within the collection of store view items 
 * @param idx Index of the specific object in the collection
 * @param qty Quantity of the store object to be updated at specific index 
 * @throws VMCSException exception on Index overflow
 */
	public void update(int idx, int qty) throws VMCSException {
		int sSize = storeCtrl.getStoreSize(type);
		if (idx >= sSize)
			throw new VMCSException("StoreViewer.update", "index overflow");
		viewItems[idx].setValue(qty);
	}

/**
 * This operation closes the store viewer
 */	
	public void closeDown() {
	}

/**
 * This operation activates the Store Display if the parameter is TRUE otherwise
 * the Store Display is deactivated
 * @param state
 */	
	public void setActive(boolean state) {
		this.setEnabled(state);
	}
	
	@Override
	public void update(Observable arg0, Object obj) {
		// TODO Auto-generated method stub
		System.out.println("Magic Observer updating all the store labels :" + arg0);
		StoreItem item = (StoreItem)obj;
		if(item != null){
			update();
		}
		
	}
}
